package com.beadhouse.nurrecord.support.service;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.beadhouse.common.dto.QueryResult;
import com.beadhouse.common.exeption.BeadHouseException;
import com.beadhouse.entity.CustomerAndNurcontentEntity;
import com.beadhouse.entity.CustomerEntity;
import com.beadhouse.entity.NurContentEntity;
import com.beadhouse.nurrecord.ao.NursingProjectAo;
import com.beadhouse.nurrecord.ao.PerSonalNursingAo;
import com.beadhouse.nurrecord.dao.NursingProjectDao;
import com.beadhouse.nurrecord.service.NursingProjectService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class NursingProjectServiceImpl implements NursingProjectService{

	@Autowired
	NursingProjectDao dao;
	
	@Override
	public void addPersonalNursingProject(NursingProjectAo ao) throws Exception {
		//1
		String customerId = ao.getCustomerId();
		String nurContentId = ao.getNurContentId();
		String startTime = ao.getStartTime();
		String content = ao.getContent();
		String nurContentName = ao.getNurContentName();
		if(customerId==null) {
			throw new BeadHouseException("客户id不能为空");
		}
		if(nurContentId==null) {
			throw new BeadHouseException("护理内容id不能为空");
		}
		if(startTime==null||startTime.equals("")) {
			throw new BeadHouseException("时间不能为空");
		}
		
		if(nurContentName==null) {
			throw new BeadHouseException("护理内容名称不能为空");
		}
		//2
		CustomerAndNurcontentEntity entity = new CustomerAndNurcontentEntity();
		int cusId = Integer.parseInt(customerId);
		entity.setCustomerId(cusId);
		entity.setDelete(false);
		int nurId = Integer.parseInt(nurContentId);
		entity.setNurContentId(nurId);
		
		//此处需要调用查询护理内容的接口
		entity.setNurContentName(nurContentName);
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		entity.setStartTime(sdf.parse(startTime));
		entity.setStatus(false);
		entity.setContent(content);
		
		dao.insert(entity);
	}

	@Override
	public void deletePersonalNursingProject(String customerId,String nurContentId) {
		if(customerId==null) {
			throw new BeadHouseException("客户id不能为空");
		}
		if(nurContentId==null) {
			throw new BeadHouseException("护理内容id不能为空");
		}
		int cusId =Integer.parseInt(customerId) ;
		int nurId = Integer.parseInt(nurContentId);
		Map<String, Object> map = new HashMap<>();
		map.put("delete", 1);
		dao.updateOne(cusId, nurId,map);
	}

	@Override
	public QueryResult<CustomerAndNurcontentEntity> selectPersonalNursingProject(PerSonalNursingAo ao) throws Exception {
		int pageNo = Integer.parseInt(ao.getPageNo()) ;
		int pageSize = Integer.parseInt(ao.getPageSize()) ;
		PageHelper.startPage(pageNo,pageSize);
		List<CustomerAndNurcontentEntity> list=dao.select("customer_id", ao.getCustomerId(),ao.getKey());
		
		PageInfo<CustomerAndNurcontentEntity> page = new PageInfo<>(list);
		QueryResult<CustomerAndNurcontentEntity> result = new QueryResult<>();
		result.setPageNo(pageNo);
		result.setPageSize(pageSize);
		result.setRows(list);
		result.setTotalRows((int)page.getTotal());
		result.setTotalPages(result.getTotalPages());
		return result;
	}

	
	@Override
	public void SetRecord(String customerId,String nurContentId) throws Exception {
		if(customerId==null) {
			throw new BeadHouseException("客户id不能为空");
		}
		if(nurContentId==null) {
			throw new BeadHouseException("护理内容id不能为空");
		}
		int cusId =Integer.parseInt(customerId) ;
		int nurId = Integer.parseInt(nurContentId);
		Map<String, Object> map = new HashMap<>();
		map.put("status", 1);
		dao.updateOne(cusId, nurId,map);
	}

	@Override
	public List<NurContentEntity> selectNurContent() throws Exception {
		List<NurContentEntity> list=dao.selectAll();
		return list;
	}

}
